package voja.android.crvenazvezda;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.RemoteViews;

public class ClockReceiver extends Service {
	private BroadcastReceiver myReceiver = new BroadcastReceiver(){
		public void onReceive(Context context, Intent intent){
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_TIME_TICK)){
				updateAppWidget(context);
			}
		}
			
		public void updateAppWidget(Context context){
			RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);
			
			updateViews.setTextViewText(R.id.txtTime, Util.getTime());
			updateViews.setTextViewText(R.id.txtDate, Util.getDate());
			
			ComponentName myComponentName = new ComponentName(context, WidgetProvider.class);
			AppWidgetManager manager = AppWidgetManager.getInstance(context);
			manager.updateAppWidget(myComponentName, updateViews);
		}
	};
	   
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	public void onCreate() {
		super.onCreate();
		
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Intent.ACTION_TIME_TICK);
		registerReceiver(myReceiver, intentFilter);
	}
	
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(myReceiver);
	}
}
